package cn.legend.travel.ask.service.impl;

import cn.legend.travel.ask.dao.persist.repository.AnswerRepository;
import cn.legend.travel.ask.dao.persist.repository.AsksRepository;
import cn.legend.travel.ask.dao.persist.repository.impl.AsksRepositoryImpl;
import cn.legend.travel.ask.pojo.entity.Answer;
import cn.legend.travel.ask.pojo.entity.Ask;
import cn.legend.travel.ask.pojo.entity.UserCollection;
import cn.legend.travel.ask.pojo.param.AddNewAnswerParam;
import cn.legend.travel.ask.pojo.vo.AnswerStandardVO;
import cn.legend.travel.ask.pojo.vo.AsksSearchVo;
import cn.legend.travel.ask.service.AnswerService;
import cn.legend.travel.ask.service.AskService;
import cn.legend.travel.common.enumerator.ServiceCode;
import cn.legend.travel.common.exception.ServiceException;
import cn.legend.travel.common.pojo.authentication.CurrentPrincipal;
import cn.legend.travel.common.pojo.vo.PageData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class AnswerServiceImpl implements AnswerService {
    @Autowired
    private AnswerRepository answerRepository;
    @Autowired
    private AsksRepository asksRepository;
    @Override
    public void addLikes(Long answerId,CurrentPrincipal currentPrincipal) {
        //TODO 和user相关的东西
       Long id = currentPrincipal.getId();
       UserCollection userCollection = asksRepository.getCollection(answerId,id,"answer");
       log.error("dadsa,{}",userCollection);
       if (userCollection != null){
           throw new ServiceException(ServiceCode.ERROR_CONFLICT,"该用户已点赞，请勿重复点赞");
       }
       UserCollection user = new UserCollection();
       user.setRate(0);
       user.setObjId(answerId);
       user.setType("answer");
       user.setContent("很不错啊");
       user.setUserId(id);
       log.error("dadsa{}",userCollection);
       int d =asksRepository.insert(user);
       if (d !=1){
           throw new ServiceException(ServiceCode.ERROR_UNKNOWN,"服务器出现异常，请稍后再试");
       }
       int i = answerRepository.addLikes(answerId);
        if (i!=1){
            throw new ServiceException(ServiceCode.ERROR_UNKNOWN,"服务器出现异常，请稍后再试");
        }
    }

    @Override
    public void cancelLikes(Long questionId, CurrentPrincipal currentPrincipal) {
        //TODO 和user相关的东西
        Long id = currentPrincipal.getId();
        UserCollection userCollection = asksRepository.getCollection(questionId,id,"answer");
        if (userCollection == null){
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,"已取消请勿重复点赞");
        }
        Answer ask = answerRepository.getAnswer(questionId);
        if (ask ==null){
            throw new ServiceException(ServiceCode.ERROR_UPLOAD_EMPTY,"该回复已经删除，请稍后再试");
        }
        int i =answerRepository.cancelLikes(questionId);
        if (i!=1){
            throw new ServiceException(ServiceCode.ERROR_UPDATE,"服务器忙，请稍后再试");
        }
    }

    @Override
    public void AddViews(Long questionId) {
        Answer ask = answerRepository.getAnswer(questionId);
        if (ask ==null){
            throw new ServiceException(ServiceCode.ERROR_UPLOAD_EMPTY,"该回复已经删除，请稍后再试");
        }
        int i = answerRepository.addView(questionId);
        if (i!=1){
            throw new ServiceException(ServiceCode.ERROR_UNKNOWN,"服务器忙，请稍后再试");
        }
    }

    @Override
    public void report(Long questionId) {
        Answer ask = answerRepository.getAnswer(questionId);
        if (ask ==null){
            throw new ServiceException(ServiceCode.ERROR_UPLOAD_EMPTY,"该回复已经删除，请稍后再试");
        }
        int i =answerRepository.report(questionId);
        if (i!=1){
            throw new ServiceException(ServiceCode.ERROR_UPDATE,"服务器忙，请稍后再试");
        }
    }

    @Override
    public void addNewAnswer(CurrentPrincipal currentPrincipal, AddNewAnswerParam addNewAnswerParam) {
        if (asksRepository.getAsk(addNewAnswerParam.getQuestionId())==null){
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,"该问题不见了，请稍后再试");
        }
        Answer answer = new Answer();
        Long id = currentPrincipal.getId();
        BeanUtils.copyProperties(addNewAnswerParam,answer);
        answer.setUserId(id);
        answer.setCheckState(1);
        answer.setLikeCount(0);
        answer.setIsRecommendation(0);
        answer.setReportTimes(0);
        int i = answerRepository.addNewAnswer(answer);
        if (i!=1){
            throw new ServiceException(ServiceCode.ERROR_INSERT,"服务器忙，请稍后再试");
        }
    }

    @Override
    public AnswerStandardVO getBestAsk(Long questionId) {
        return answerRepository.getBestAsk(questionId);
    }

    @Override
    public PageData<AsksSearchVo> search(String keyword, Integer page) {
        return null;
    }

}
